var PAGE = (function() {
    var Version = 0.1,
        W3C = document.dispatchEvent, //IE9开始支持W3C的事件模型与getComputedStyle取样式值
        //移动端页面，以rem为单位时
        REM = {
            /**
             *@param base 750/100  750宽度的设计稿对应100px，即1rem=100px
             */
            adapt: function(base) {
                var bw = document.documentElement.offsetWidth,
                    fs = bw / base + 'px';
                document.documentElement.style.fontSize = fs;
                window.addEventListener('onorientationchange' in window ? 'orientationchange' : 'resize', function() {
                    var bw = document.documentElement.offsetWidth,
                        fs = bw / base + 'px';
                    document.documentElement.style.fontSize = fs;
                });
            },
            /**
             *@param cb1竖屏回调
             *@param cb2横屏回调
             *@des 0 180竖屏  -90 90 横屏
             */
            screenEvt: function(cb1, cb2) {
                if (window.orientation == 0 || window.orientation == 180) {
                    cb1 && cb1()
                } else if (window.orientation == -90 || window.orientation == 90) {
                    cb2 && cb2()
                }
            }
        },
        UA = {
            ua: window.navigator.userAgent,
            isWeiXin: function() {
                return /micromessenger/i.test(this.ua)
            },
            isIos: function() {
                return /iphone|ios/i.test(this.ua)
            },
            isAndroid: function() {
                return /android/i.test(this.ua)
            },
            isMobile: function() {
                var Agents = new Array("Android", "iPhone", "SymbianOS", "Windows Phone", "iPad", "iPod");
                for (var v = 0; v < Agents.length; v++) {
                    if (this.ua.indexOf(Agents[v]) > 0) {
                        return true;
                    }
                }
                return false;
            },
            isIE678: !+"\v1"
        },
        TOUCH = {
            hasTouch: !!('ontouchstart' in window || window.DocumentTouch && document instanceof window.DocumentTouch)
        },
        CSS = {
            createStyle: function(csstext) {
                var new_style;
                document.all ? (new_style = document.createStyleSheet(), new_style.cssText = csstext) :
                    (new_style = document.createElement('style'), new_style.type = 'text/css', new_style.textContent = csstext);
                try {
                    document.getElementsByTagName('head')[0].appendChild(new_style);
                } catch (o) {}
                e = null
            },
            loadCss: function(cssUrl) {
                var link;
                document.createStyleSheet ? document.createStyleSheet(cssUrl) :
                    (link = document.createElement('link'), link.rel = 'stylesheet', link.type = 'text/css', link.href = cssUrl, document.getElementsByTagName('head')[0].appendChild(link));
            }
        },
        SCRIPT = {
            createScript: function() {},
            loadScript: function(url, cb) {
                var script = document.createElement('script'),
                    head = document.getElementsByTagName('head')[0];
                script.type = 'text/javascript';
                script[W3C ? 'onload' : 'onreadystatechange'] = function() {
                    if (W3C || /loaded|complete/i.test(script.readyState)) {
                        cb && cb()
                    }
                };
                script.src = url;
                //插入到head的第一个节点前，防止IE6下head标签没闭合前使用appendChild抛错
                head.insertBefore(script, head.lastChild); //chrome下第二个参数不能为null
                // document.getElementsByTagName('head')[0].appendChild(script);
            }
        };
    /**
     * Request Animation Frame Polyfill.
     * @author Tino Zijdel
     * @author Paul Irish
     * @see https://gist.github.com/paulirish/1579671
     */
    function setRaf() {
        var lastTime = 0;
        var vendors = ['ms', 'moz', 'webkit', 'o'];

        for (var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) {
            window.requestAnimationFrame = window[vendors[x] + 'RequestAnimationFrame'];
            window.cancelAnimationFrame = window[vendors[x] + 'CancelAnimationFrame'] || window[vendors[x] + 'CancelRequestAnimationFrame'];
        }

        if (!window.requestAnimationFrame) {
            window.requestAnimationFrame = function(callback, element) {
                var currTime = new Date().getTime();
                var timeToCall = Math.max(0, 16 - (currTime - lastTime));
                var id = window.setTimeout(function() {
                        callback(currTime + timeToCall);
                    },
                    timeToCall);
                lastTime = currTime + timeToCall;
                return id;
            };
        }

        if (!window.cancelAnimationFrame) {
            window.cancelAnimationFrame = function(id) {
                clearTimeout(id);
            };
        }
    };

    function doDomReady(cb) {
        document.addEventListener('DOMCotentLoaded', function() {
            cb && cb()
        })
    };
    return {
        setRem: REM.adapt,
        uaInfo: {
            isWeiXin: UA.isWeiXin(),
            isIos: UA.isIos(),
            isAndroid: UA.isAndroid(),
            isMobile: UA.isMobile(),
            isIE678: UA.isIE678
        },
        touchInfo: {
            hasTouch: TOUCH.hasTouch
        },
        CSS: CSS,
        SCRIPT: SCRIPT,
        Ready: doDomReady
    }
}());
